package com.qinyi.framework.shiro.realm;

import com.qinyi.framework.shiro.service.SysLoginService;
import com.qinyi.system.domin.SysUser;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author wangan on 2019/6/4
 * @description
 */
public class UserRealm extends AuthorizingRealm {

	@Autowired
	private SysLoginService loginService;

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
		String username = usernamePasswordToken.getUsername();
		String password = new String(usernamePasswordToken.getPassword());
		SysUser user = loginService.login(username, password);
		SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, password, getName());
		return authenticationInfo;
	}
}
